﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using WxPay;
using NS.BLL.Systems;
using NS.Utilities;
using BLL;
using Model;

public partial class wechat_ResultNotifyPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ResultNotify resultNotify = new ResultNotify(this);
        //在页面上显示订单信息

        WxPayData getNotifyData = resultNotify.GetNotifyData();
        if (getNotifyData.GetValue("return_code").ToString() == "SUCCESS" && getNotifyData.GetValue("result_code").ToString() == "SUCCESS")
        {
            string time_end = DateTime.Now.ToString("G");
            string TradeNo = getNotifyData.GetValue("out_trade_no").ToString();
            string PayNo = getNotifyData.GetValue("transaction_id").ToString();
            if (getNotifyData.GetValue("attach").ToString() == "0")
            {
                if (!OrderService.OrderIsPay(TradeNo))
                {
                    if (UtilityService.UpdateFieldByPK("Order", "PayNo,PayDate,State", new Object[] { PayNo, time_end, 1 }, "TradeNo", TradeNo) > 0)
                    {
                        Log.Info(this.GetType().ToString(), "交易单号:" + TradeNo + "，订单更新成功");
                    }
                    else
                    {
                        Log.Info(this.GetType().ToString(), "交易单号:" + TradeNo + "，订单更新失败");
                    }
                }
            }
            else
            {
                if (CouponsTradeService.GetTradeCount(PayNo) == 0)
                {
                    if (UtilityService.UpdateFieldByPK("CouponsTrade", "PayNo,PayDate,State", new Object[] { PayNo, time_end, 1 }, "TradeNo", TradeNo) > 0)
                    {
                        //此处新订单提醒换成购物券提醒
                        //OrderService.NewOrderMessage(getNotifyData.GetValue("out_trade_no").ToString());
                        CouponsScheme C = CouponsSchemeService.GetCouponsSchemeBySchemeID(Convert.ToInt32(getNotifyData.GetValue("attach")));
                        Log.Info(this.GetType().ToString(), "洗车券数量:" + C.Quantity);
                        CouponsTrade T = CouponsTradeService.GetCouponsTradeByTradeNo(TradeNo);
                        if (!string.IsNullOrEmpty(T.PayNo))
                        {
                            try
                            {
                                //添加消费记录
                                if (!MemberPayService.IsExistTradeNo(T.PayNo))
                                {
                                    MemberPay mp = new MemberPay();
                                    mp.OpenID = T.OpenID;
                                    mp.Money = T.Money;
                                    mp.TradeNo = T.PayNo;
                                    mp.Date = DateTime.Now;
                                    MemberPayService.AddMemberPay(mp);
                                }
                            }
                            catch (Exception ex)
                            {
                                Log.Info(this.GetType().ToString(), "添加消费记录出错:" + ex.Message);
                            }
                        }

                        DateTime yxrq = DateTime.Now.AddDays(C.ValidDays);
                        Log.Info(this.GetType().ToString(), "保质期:" + yxrq.ToString());
                        //循环中的money应该为多少
                        for (int i = 0; i < C.Quantity; i++)
                        {
                            Log.Info(this.GetType().ToString(), "插入洗车券:" + i.ToString());
                            try
                            {
                                UtilityService.InsertField("Coupons", "OpenID,Money,Explain,YouXQStart,YouXQEnd,CouType,IsUse,TradeID", new Object[] { getNotifyData.GetValue("openid").ToString(), 0, C.Explain, time_end, yxrq, 1, 0, Convert.ToInt32(T.TradeID) });
                            }
                            catch (Exception ex)
                            {
                                Log.Info(this.GetType().ToString(), "插入数据出错:" + ex.Message);
                            }
                            Log.Info(this.GetType().ToString(), "系统交易单号" + T.TradeID + "，洗车券插入成功");
                        }

                        Log.Info(this.GetType().ToString(), "交易单号" + getNotifyData.GetValue("out_trade_no").ToString() + "，洗车券订单更新成功");
                    }
                    else
                    {
                        Log.Info(this.GetType().ToString(), "交易单号" + getNotifyData.GetValue("out_trade_no").ToString() + "，洗车券订单更新失败");
                    }
                }
            }

            time_end = DateTime.ParseExact(getNotifyData.GetValue("time_end").ToString(), "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture).ToString();//string.Format("{0:G}", getNotifyData.GetValue("time_end"));
            Log.Info(this.GetType().ToString(), "支付成功，微信订单号：" + getNotifyData.GetValue("transaction_id") + "、支付时间：" + time_end + "、商户订单号：" + getNotifyData.GetValue("out_trade_no").ToString());

            WxPayData res = new WxPayData();
            res.SetValue("return_code", "SUCCESS");
            res.SetValue("return_msg", "OK");
            Response.Write(res.ToXml());
            Response.End();
        }
        else
        {
            WxPayData res = new WxPayData();
            res.SetValue("return_code", "FAIL");
            res.SetValue("return_msg", "支付失败");
            Log.Error(this.GetType().ToString(), "支付失败");
            Response.Write(res.ToXml());
            Response.End();
        }
        //resultNotify.ProcessNotify();
    }
}